"use client";
import ComContainer from "@/components/ComContainer";
import PrimaryList from "@/components/List";
import React, { useEffect, useState } from "react";
import { DataType } from "../page";
import { delPost, getOwnPost, getPosts, getUserById } from "@/api";
import { message } from "antd";
import { useRouter } from "next/navigation";
import { useStore } from "@/zustand/store";

export default function PersonalPost() {
  const [data, setData] = useState<DataType[]>([]);
  const [loading, setLoading] = useState<boolean>(true);
  const [total, setTotal] = useState(0);
  const router = useRouter();

  //TODO: 获取自己的帖子
  const getPostList = async (pageNum = 1, pageSize = 10) => {
    const res = await getOwnPost({
      pageNum,
      pageSize,
    });
    if (res.data.code === 200) {
      setTotal(res.data.data.total);
      setData(res.data.data.records);
      setLoading(false);
    } else {
      message.error(res.data.msg);
    }
  };
  const handleClick = (id: number) => {
    router.push(`/PostContentView?postId=${id}`);
  };
  const handleEdite = (id: number) => {
    router.push(`/PublishPost?postId=${id}`);
  };
  const handleDelete = async (id: number) => {
    //  删除帖子
    const res = await delPost([id]);
    if (res.data.code === 200) {
      getPostList();
      message.success("删除成功");
    } else {
      message.error(res.data.msg);
    }
  };

  return (
    <ComContainer ContentWidth={800}>
      <PrimaryList
        getList={getPostList}
        data={data}
        loading={loading}
        total={total}
        handleClick={handleClick}
        isShowStatus={true}
        handleEdite={handleEdite}
        handleDelete={handleDelete}
      ></PrimaryList>
    </ComContainer>
  );
}
